package com.sicnu.cheer.leaugebar.activity;

import android.app.ActivityOptions;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.R;
import com.sicnu.cheer.leaugebar.activity.profileset.SexSetActivity;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.util.ValidateUtil;

import butterknife.InjectView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * Created by cheer on 2016/10/23.
 * 登录页，实现用户名密码验证
 */
public class LoginActivity extends BaseActivity {
    @InjectView(R.id.et_username)
    EditText etUsername;
    @InjectView(R.id.et_password)
    EditText etPassword;
    @InjectView(R.id.bt_go)
    Button btGo;
    @InjectView(R.id.cv)
    CardView cv;
    @InjectView(R.id.fab)
    FloatingActionButton fab;
    @InjectView(R.id.forget_password_tv)
    TextView fpt;

    private Intent registerIntent;

    @Override
    public int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        super.initData();
        registerIntent = new Intent(this, RegisterOrResetActivity.class);
        BmobUser currentUser = BmobUser.getCurrentUser(mThis);

        if (currentUser!=null){
            Intent intent=new Intent(mThis,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 点击登录、注册、忘记密码时
     * @param view
     */
    @OnClick({R.id.bt_go, R.id.fab,R.id.forget_password_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_password_tv:
                getWindow().setExitTransition(null);
                getWindow().setEnterTransition(null);

                registerIntent.putExtra("isRegister",false);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityOptions options =
                            ActivityOptions.makeSceneTransitionAnimation(this, fab, fab.getTransitionName());
                    startActivity(registerIntent, options.toBundle());
                } else {
                    startActivity(registerIntent);
                }
                break;
            case R.id.fab:
                try {
                    getWindow().setExitTransition(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getWindow().setEnterTransition(null);

                registerIntent.putExtra("isRegister",true);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityOptions options =
                            ActivityOptions.makeSceneTransitionAnimation(this, fab, fab.getTransitionName());
                    startActivity(registerIntent, options.toBundle());
                } else {
                    startActivity(registerIntent);
                }
                break;
            case R.id.bt_go:
                doLogin();
                break;
        }
    }

    private void doLogin() {
        String username=etUsername.getText().toString();
        String password=etPassword.getText().toString();
        if (StringUtils.isEmpty(username)){
            UIHelper.ToastMessage(mThis,"请输入用户名（手机号）！");
            return;
        }

        if (!ValidateUtil.isMobile(username)) {
            UIHelper.ToastMessage(mThis,"请输入正确的手机号！");
            return;
        }
        if (StringUtils.isEmpty(etPassword.getText().toString())){
            UIHelper.ToastMessage(mThis,"请输入用户名密码！");
            return;
        }
        //Bmob的登录功能
        final ProgressDialog dialog=new ProgressDialog(mThis);
        dialog.setMessage("登录中...");
        dialog.show();
        BmobUser.loginByAccount(mThis, username, password, new LogInListener<MyUser>() {
            @Override
            public void done(MyUser user, BmobException e) {
                dialog.dismiss();
                if(user!=null){
                    UIHelper.ToastMessage(mThis,"用户登录成功");
                    Intent i2 = new Intent(mThis,SexSetActivity.class);
                    startActivity(i2);
                    finish();
                }else {
                    UIHelper.ToastMessage(mThis,"登录失败:"+e.getLocalizedMessage());
                }
            }
        });

    }
}
